package com.wuneng.commonlibrary.hardware.bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.provider.Settings;
import android.util.Log;

import java.util.Set;

public class CariBluetooth {
    private Context context;

    private CariBluetoothCallBack callBack;

    private BluetoothAdapter bluetoothAdapter;

    private IntentFilter intentFilter;

    private CariBluetoothReceiver receiver;

    private BluetoothConnect bluetoothConnect;



    public CariBluetooth(Context context, CariBluetoothCallBack callBack) {
        this.context = context;
        this.callBack = callBack;
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        intentFilter = new IntentFilter();
        receiver = new CariBluetoothReceiver();
        bluetoothConnect = new BluetoothConnect(context,callBack);
        init();
    }

    private void init(){
        intentFilter.addAction("android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED");
        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        intentFilter.addAction("android.bluetooth.BluetoothAdapter.STATE_OFF");
        intentFilter.addAction("android.bluetooth.BluetoothAdapter.STATE_ON");
        context.registerReceiver(receiver,intentFilter);
    }

    public void startBluetooth(){
        if(bluetoothAdapter.isEnabled()){
            getBoundedDevice();
        }else{
            bluetoothAdapter.enable();
        }
    }

    private Set<BluetoothDevice> getBoundedDevice(){

        Set<BluetoothDevice> bluetoothDeviceSet = bluetoothAdapter.getBondedDevices();

        if(bluetoothDeviceSet.size()>0){
            bluetoothConnect.connect((BluetoothDevice) bluetoothDeviceSet.iterator().next());
        }else{
            Intent intent1 = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
            context.startActivity(intent1);
        }
        return  bluetoothDeviceSet;
    }


    public void release(){
        bluetoothConnect.stop();
        context.unregisterReceiver(receiver);
    }

    public void writeData(byte[] buffer){
        bluetoothConnect.write(buffer);
    }

    private class CariBluetoothReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                        BluetoothAdapter.ERROR);
                switch (state) {
                    case BluetoothAdapter.STATE_OFF:
                        Log.d("aaa", "STATE_OFF 手机蓝牙关闭");
                        callBack.onBluetoothClose();
                        break;
                    case BluetoothAdapter.STATE_TURNING_OFF:
                        Log.d("aaa", "STATE_TURNING_OFF 手机蓝牙正在关闭");
                        break;
                    case BluetoothAdapter.STATE_ON:
                        Log.d("aaa", "STATE_ON 手机蓝牙开启");
                        callBack.onBluetoothOpen();
                        getBoundedDevice();
                        break;
                    case BluetoothAdapter.STATE_TURNING_ON:
                        Log.d("aaa", "STATE_TURNING_ON 手机蓝牙正在开启");
                        break;
                }
            }

            if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String name = device.getName();
                int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);
                switch (state) {
                    case BluetoothDevice.BOND_NONE:
                        Log.d("aaa", "BOND_NONE 删除配对");
                        break;
                    case BluetoothDevice.BOND_BONDING:
                        Log.d("aaa", "BOND_BONDING 正在配对");
                        break;
                    case BluetoothDevice.BOND_BONDED:
                        Log.d("aaa", "BOND_BONDED 配对成功");
                        callBack.onBoundSuccess(device);
                        getBoundedDevice();
                        break;
                }
            }

            if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                callBack.onDeviceConnect(device);
            } else if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                callBack.onDeviceDisconnect(device);
            }
        }
    }
}
